package com.nb.nike.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.example.project1762.Helper.ManagmentCart
import com.nb.nike.R
import com.nb.nike.adapter.ColorAdapter
import com.nb.nike.adapter.SizeAdapter
import com.nb.nike.adapter.SliderAdapter
import com.nb.nike.databinding.ActivityDetailBinding
import com.nb.nike.model.ItemsModel
import com.nb.nike.model.SliderModel

class DetailActivity : BaseActivity() {
    private lateinit var binding:ActivityDetailBinding
    private lateinit var item:ItemsModel
    private var numberOder = 1
    private lateinit var managmentCart: ManagmentCart
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)

        managmentCart = ManagmentCart(this)
        getBundle()
        banners()
        initList()
    }
    private fun initList(){
        val sizList = ArrayList<String>()
        for (size in item.size){
            sizList.add(size.toString())
        }

        binding.sizeList.adapter = SizeAdapter(sizList)
        binding.sizeList.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)


        val colorList = ArrayList<String>()
        for (imageUrl in item.picUrl){
            colorList.add(imageUrl)
        }

        binding.colorList.adapter = ColorAdapter(colorList)
        binding.colorList.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)

    }
    private fun banners() {
        var sliderItem = ArrayList<SliderModel>()
        for(imageUrl in item.picUrl){
            sliderItem.add(SliderModel(imageUrl))
        }
        binding.slider.adapter = SliderAdapter(sliderItem,binding.slider)
        binding.slider.clipToPadding = true

        binding.slider.clipChildren = true
        binding.slider.offscreenPageLimit =1

        if(sliderItem.size>1){
            binding.dotIndicator.visibility = View.VISIBLE
            binding.dotIndicator.attachTo(binding.slider)

        }
    }

    private fun getBundle() {

        item = intent.getParcelableExtra("object")!!

        binding.titleTxt.text = item.title
        binding.desciptionTxt.text = item.description
        binding.priceTxt.text  = item.price.toString()
        binding.ratingTxt.text = "${item.rating}"

        binding.addToCartBtn.setOnClickListener{
            item.numberInCart = numberOder

            managmentCart.insertFood(item)
        }

        binding.backBtn.setOnClickListener{
            finish()
        }

        binding.cartBtn.setOnClickListener{
            startActivity(Intent(this@DetailActivity,CartActivity::class.java))
        }


    }
}